/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.clarice.homechef.dao;

import com.clarice.homechef.domain.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Akshay Velhal
 */
@Repository
public class UserDaoImpl implements UserDao{
    
    @Autowired
    DataSource dataSource;
    
    private static String getUser = "select userid, name, email, area, address, mobile from users where email = ?";
    
    public User getUser(String email) throws Exception {
        User user = new User();
        try (Connection conn = dataSource.getConnection();
                PreparedStatement ps = conn.prepareStatement(getUser);) {
            
            ps.setString(1, email);
            ResultSet rs = ps.executeQuery();
            while(rs.next()) {
                user.setUserId(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                user.setEmail(rs.getString("email"));
                user.setArea(rs.getString("area"));
                user.setAddress(rs.getString("address"));
                user.setMobile(rs.getString("mobile"));
            }
        } catch(Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return user;
    }
    
}
